//
//  Enemy.cpp
//  Space shooting
//
//  Created by Raiden on 9/12/13.
//
//

#include "Enemy.h"
#include "Game.h"
#include "Constants.h"
#include "SimpleAudioEngine.h"

Enemy::Enemy() {    
    explotion = NULL;
    size = CCDirector::sharedDirector()->getWinSize();
}

Enemy::~Enemy() {
    remove();
}

void Enemy::selfExplotion() {
    if(CCUserDefault::sharedUserDefault()->getBoolForKey(SOUND_SETTING_KEY)) {
        CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("explotion01.wav");
    }

    explotion = CCMWSprite::create("explotion01.anu", 0, "explotion01.png", NULL);
    explotion->setPosition(getPosition());
    
    // Add explotion to parent scene
    getParent()->addChild(explotion);
    
    // Hide enemy
    setVisible(false);
    
}

void Enemy::update(float delta) {
    
    if(hp <= 0 && explotion == NULL) {        
        
        EnemyList->removeObject(this);
        selfExplotion();
    }
    
    if(explotion != NULL && explotion->isAnimationEnded()) {
        
        // Remove explotion from parent scene
        getParent()->removeChild(explotion);
        
        // Insert orb
        addBulletOrb(explotion->getPosition());
        
        // Add score ord
        addScoreParitcle(explotion->getPosition());
        
        // Remove enemy from parent scene
        remove();        
    }
}

void Enemy::addScoreParitcle(CCPoint point) {
    
    CCParticleSystemQuad* scoreOrb = CCParticleSystemQuad::create("scoreOrb.plist");
    scoreOrb->setPosition(point);
    
    // Bezier move
    ccBezierConfig config;
    config.controlPoint_1 = scoreOrb->getPosition();
    config.controlPoint_2 = ccp(scoreOrb->getPosition().x, 480);
    config.endPosition = Game::getInstance()->scorePoint;
        
    // Move bullet to top of screen
    CCFiniteTimeAction *moveParticle = CCBezierTo::create(1.0, config);

    // Call remove bullet after move done
    CCFiniteTimeAction *moveDone = CCCallFuncN::create(this, callfuncN_selector(Enemy::removeParticle));

    scoreOrb->runAction(CCSequence::create(moveParticle, moveDone, NULL));
    
    MainScene->addChild(scoreOrb);
}

void Enemy::addBulletOrb(CCPoint point) {
    
    orb = CCMWSprite::create("bulletOrb.anu", 0, "bulletOrb.png", NULL);
    orb->setPosition(explotion->getPosition());
    orb->setLoopCount(-1);
    orb->setScale(0.5);
    
    // Add to list for checl collise
    OrbList->addObject(orb);   
    
    // Move bullet to top of screen
    CCFiniteTimeAction *moveParticle = CCMoveTo::create(10.0, ccp(point.x, - (size.height - point.y)));
    
    // Call remove bullet after move done
    CCFiniteTimeAction *moveDone = CCCallFuncN::create(this, callfuncN_selector(Enemy::removeBulletOrb));
    
    orb->runAction(CCSequence::create(moveParticle, moveDone, NULL));   
    
    MainScene->addChild(orb);
}

void Enemy::removeParticle(CCObject *sender) {
 
    CCParticleSystemQuad *particle = (CCParticleSystemQuad*) sender;
    MainScene->removeChild(particle, true);

}

void Enemy::removeBulletOrb(CCObject *sender) {
    
    CCMWSprite *particle = (CCMWSprite*) sender;
    MainScene->removeChild(particle, true);
    OrbList->removeObject(particle);
}

void Enemy::move() {
    
    CCFiniteTimeAction *move = CCMoveTo::create(speed, ccp(getPosition().x, - getContentSize().height));
    CCFiniteTimeAction *remove = CCCallFuncN::create(this, callfuncN_selector(Enemy::remove));
    
    runAction(CCSequence::create(move, remove, NULL));
}

void Enemy::remove() {
    removeFromParentAndCleanup(true);
}